import { ref } from 'vue';
import api from 'axios';
import { cataItem } from 'src/hook/common/useCatalogType';
import { scaCatalogType, scaCatalogUrlAPI } from 'src/util/ScaBaseConst';


export function useScaCatalogTbl () {
  const scaProjectList = ref<cataItem[]>([]);//项目列表
  const scaPageList = ref<cataItem[]>([]);//页面列表
  const scaVariableList = ref<cataItem[]>([]);//变量列表
  const scaDeviceList = ref<cataItem[]>([]);//设备列表

  //路径
  let url = ref('');

  //从后台读取目录
  const getScaCatalog = (catalog: scaCatalogType) => {
    switch (catalog) {
      case scaCatalogType.PROJECT_CODE:
        url.value = scaCatalogUrlAPI.PROJECT_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            scaProjectList.value = res.data.data;
          }
        })
        break;
      case scaCatalogType.DEVICE_CODE:
        url.value = scaCatalogUrlAPI.DEVICE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            scaDeviceList.value = res.data.data;
          }
        })
        break;
      case scaCatalogType.VARIABLE_CODE:
        url.value = scaCatalogUrlAPI.VARIABLE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            scaVariableList.value = res.data.data;
          }
        })
        break;
      case scaCatalogType.PAGE_CODE:
        url.value = scaCatalogUrlAPI.PAGE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            scaPageList.value = res.data.data;
          }
        })
        break;
      default:
        break;
    }
  };


  //释放目录
  const removeScaCatalog = (catalog: scaCatalogType) => {
    switch (catalog) {
      case scaCatalogType.PROJECT_CODE:
        scaProjectList.value.length = 0;
        break;
      case scaCatalogType.DEVICE_CODE:
        scaDeviceList.value.length = 0;
        break;
      case scaCatalogType.VARIABLE_CODE:
        scaVariableList.value.length = 0;
        break;
      case scaCatalogType.PAGE_CODE:
        scaPageList.value.length = 0;
        break;
      default:
        break;
    }
  }
  return {
    scaProjectList,
    scaDeviceList,
    scaVariableList,
    scaPageList,

    getScaCatalog,
    removeScaCatalog
  }
}
